using System;
using System.Collections.Generic;
using System.Text;
using LispInterpretor.HelperClasses;
using LispInterpretor.DataTypes;

namespace LispInterpretor.InBuiltProcedures
{
    internal class Subtract : Procedure
    {
        private const string CANT_SUBTRACT_NONE_NUMBERS = "Can't subtract arguments that are not numbers.";

        public override object Apply(params object[] args)
        {
            Check.IsTrue(args.Length >= 2, "Subtraction requires at least two formal parameters");

            Check.IsTrue(args[0] is Number, CANT_SUBTRACT_NONE_NUMBERS);

            Number accumulator = (Number)args[0];

            for (int i = 1; i < args.Length; i++)
            {
                Check.IsTrue(args[i] is Number, CANT_SUBTRACT_NONE_NUMBERS);

                accumulator = Number.Subtract(accumulator, (Number) args[i]);
            }

            return accumulator;
        }
    }
}
